package com.rongke.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期帮助类
 *
 * @author rplees
 * @date 2010-10-18
 */
public class DateUtils {

    public final static String YYYY_MM_DD_HH_MM    = "yyyy-MM-dd HH:mm";
    public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public final static String DEFAULT_DATE_FORMAT = YYYY_MM_DD_HH_MM_SS;
    public final static String YYYYMMDDHHMMSS      = "yyyyMMddHHmmss";
    public final static String YYYY_MM_DD          = "yyyy-MM-dd";
    public final static String YYYYMMDD            = "yyyyMMdd";

    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }

    public static Date formatDate(String dateStr, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(dateStr);
            return date;
        } catch (ParseException e) {
            return null;
        }
    }

    public static String parseDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static boolean checkDateStrValid(String dateStr) {
        if (StringUtil.isEmpty(dateStr)) {
            return false;
        }

        Date date = formatDate(dateStr, "yyyyMMdd");
        if (null == date) {
            return false;
        }

        String newDateStr = parseDate(date, "yyyyMMdd");
        return dateStr.equals(newDateStr);
    }

    /**
     * 获取当年第一天
     */
    public static Date getYearFirstDay() {
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(year + "-01-01");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 当前时间格式化
     *
     * @return YYYY MM DD
     */
    public static String nowDateSimple() {
        return dateSimple(new Date());
    }

    /**
     * 当前时间格式化
     *
     * @return YYYY MM DD
     */
    public static String dateSimple(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
        return simpleDateFormat.format(date);
    }

    /**
     * 将string 格式字符串转换成long型
     */
    public static Long verifyDateformString(String s) throws ParseException {
        return getDateFormat(YYYY_MM_DD_HH_MM_SS).parse(s).getTime();
    }

    public static Date getTodayStartDate() {
        return getDayStartDate(new Date());
    }

    /**
     * 转时间
     *
     * @param f
     *            格式化
     * @param date
     *            时间
     * @return date类型
     */
    public static Date parse(String f, String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(f);
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Date getDayStartDate(Date date) {
        Calendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);

        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        return currentDate.getTime();
    }

    public static Date getDayEndDate(Date date) {
        Calendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);

        currentDate.set(Calendar.HOUR_OF_DAY, 24);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        return currentDate.getTime();
    }

    public static Date getTodayEndDate() {
        return getDayEndDate(new Date());
    }

    public static Date getWeekStartDate() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH),
            0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    public static Date getWeekEndDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getWeekStartDate());
        cal.add(Calendar.DAY_OF_WEEK, 7);
        return cal.getTime();
    }

    public static Date getMonthStartDate() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH),
            0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    public static Date getMonthEndDate() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH),
            0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTime();
    }

    /**
     * 验证是不是有效的日期
     *
     * @param hrs
     * @return
     */
    public static boolean verifyDateRule(DayRule dr, DateRule... hrs) {
        DayRule[] drs = new DayRule[1];
        drs[0] = dr;

        return verifyDateRule(drs, hrs);
    }

    /**
     * 验证是不是有效的日期
     *
     * @return
     */
    public static boolean verifyDateRule(DayRule dr) {
        DayRule[] drs = new DayRule[1];
        drs[0] = dr;
        return verifyDateRule(drs);
    }

    /**
     * 验证是不是有效的日期
     *
     * @param dys
     * @param hrs
     * @return
     */
    public static boolean verifyDateRule(DayRule[] dys, DateRule... hrs) {
        Date d = new Date();

        boolean f = false;
        if (dys != null && dys.length > 0) {
            for (DayRule dr : dys) {
                if (dr.between(d))
                    f = true;
                if (f)
                    break;
            }
        } else {
            f = true;
        }

        // 天数验证不通过，就没必要验证小时了
        if (!f)
            return f;

        if (hrs == null || hrs.length < 1)
            return true;
        for (DateRule hr : hrs) {
            if (hr.between(d))
                return true;
        }

        return false;
    }

    /**
     * 在现在时间的基础上增加 days 后的日期
     *
     * @param days
     * @return
     */
    public static Date dayAdd(int days) {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.set(Calendar.DAY_OF_YEAR, day + days);
        return calendar.getTime();
    }

    /**
     * 在指定时间的基础上增加 hour 后的日期
     *
     * @param hour
     * @return
     */
    public static Date hourAdd(int hour, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        calendar.set(Calendar.HOUR_OF_DAY, hours + hour);
        return calendar.getTime();
    }

    /**
     * 在现在时间的基础上增加 days 后的日期
     */
    public static Date hourAdd(int hour) {
        Calendar calendar = Calendar.getInstance();
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        calendar.set(Calendar.HOUR_OF_DAY, hours + hour);
        return calendar.getTime();
    }

    /**
     * 在指定时间的基础上增加 days 后的日期
     *
     * @param days
     * @return
     */
    public static Date dayAdd(int days, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.set(Calendar.DAY_OF_YEAR, day + days);
        return calendar.getTime();
    }

    /**
     * 在指定时间的基础上增加 days 后的日期
     *
     * @param days
     * @return
     */
    public static String dayAdd(int days, String date) {
        try {
            return getDateFormat().format(DateUtils.dayAdd(days, parse(date)));
        } catch (Exception e) {
            return null;
        }
    }

    public static Date parse(String date) {
        try {
            return getDateFormat().parse(date);
        } catch (Exception e) {
            try {
                return getDateFormat(YYYY_MM_DD_HH_MM_SS).parse(date);
            } catch (Exception e1) {
                try {
                    return getDateFormat(YYYY_MM_DD_HH_MM).parse(date);
                } catch (ParseException e2) {
                    try {
                        return getDateFormat(YYYYMMDDHHMMSS).parse(date);
                    } catch (Exception e3) {
                        try {
                            return getDateFormat(YYYY_MM_DD).parse(date);
                        } catch (Exception e4) {
                            try {
                                return getDateFormat("HH:mm:ss").parse(date);
                            } catch (Exception e5) {
                                return null;
                            }
                        }
                    }
                }
            }
        }
    }

    public static int getNowDayOfWeek() {
        return getDayOfWeek(new Date());
    }

    /**
     * 描述
     *
     * @param dt
     * @return 0 - 礼拜7
     */
    public static int getDayOfWeek(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return w;
    }

    // 月数操作
    public static Date monthAdd(int months) {
        Calendar calendar = Calendar.getInstance();
        int m = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, m + months);
        return calendar.getTime();
    }

    // 月数操作
    public static Date monthAdd(int months, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int m = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, m + months);
        return calendar.getTime();
    }

    public static Date minAdd(int mins) {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(12);
        calendar.set(12, day + mins);
        return calendar.getTime();
    }

    public static Date minAdd(int hour, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hours = calendar.get(12);
        calendar.set(12, hours + hour);
        return calendar.getTime();
    }

    /**
     * 根据DateFormat 格式化现在时间
     *
     * @param df
     * @return 如:2012-12-21
     */
    public static String nowDate(DateFormat df) {
        if (null == df) {
            df = getDateFormat();
        }
        return df.format(new Date());
    }

    /**
     * 默认的格式化时间
     *
     * @return 2012-12-21 12:31:20
     */
    public static String nowDate() {
        return getDateFormat().format(new Date());
    }

    /**
     * 默认的格式化时间
     *
     * @return 2012-12-21 12:31:20
     */
    public static String nowDate(String format) {
        return getDateFormat(format).format(new Date());
    }

    public static DateFormat getDateFormat() {
        return new SimpleDateFormat(DEFAULT_DATE_FORMAT);
    }

    public static DateFormat getDateFormat(String format) {
        return new SimpleDateFormat(format);
    }

    public static String format(String format, Date date) {
        return getDateFormat(format).format(date);
    }

    /**
     * 是否为同一个月份
     *
     * @param d1
     * @param d2
     * @return Boolean.True 同一个
     */
    public static boolean isSameMonth(Date d1, Date d2) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(d1);
        int i1 = instance.get(Calendar.MONTH);

        instance.setTime(d2);
        int i2 = instance.get(Calendar.MONTH);

        return i1 == i2;
    }

    /**
     * 是否为同一个天数
     *
     * @param d1
     * @param d2
     * @return Boolean.True 同一个
     */
    public static boolean isSameDay(Date d1, Date d2) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(d1);
        int i1 = instance.get(Calendar.DAY_OF_YEAR);

        instance.setTime(d2);
        int i2 = instance.get(Calendar.DAY_OF_YEAR);

        return i1 == i2;
    }

    /**
     * 格式化时间 YYYY_MM_DD_HH_MM_SS 2015-11-24 17:12:00 YYYY_MM_DD_HH_MM 2015-11-24
     * 17:12 data 2015-11-24 17:12
     *
     * @return 2015-11-24 17:12:00格式的时间
     */
    public static String timeDate(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty())
            ;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }

    /**
     * 格式化时间 YYYY_MM_DD_HH_MM_SS 2015-11-24 17:12:00 YYYY_MM_DD_HH_MM 2015-11-24
     * 17:12 data 2015-11-24 17:12 date 时间 format 格式形式
     * 
     * @return 2015-11-24 17:12:00格式的时间
     */
    public static String timeDate(Date date, String format) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /****
    * @param date1
    *            <String>
    * @param date2
    *            <String>
    * @return int
    * @throws ParseException
    */
    public static int getMonthSpace(Date date1, Date date2) throws ParseException {
        int result = 0;
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(date1);
        c2.setTime(date2);
        result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH);
        return result == 0 ? 1 : Math.abs(result);
    }

    /**
     * 获取传入日期年的第几个季度
     *
     * @param date
     *            日期
     * @return 此年份地几个季度
     */
    public static int getQuarterOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) / 3 + 1;
    }

    /**
     * 获取一个月份的季度开始月份
     *
     * @param month
     *            月份
     * @return 开始月份
     */
    public static Integer getQuarterStartMonth(Integer month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date currentQuarterStartTime;
        try {
            currentQuarterStartTime = getCurrentQuarterStartTime(sdf.parse("2000-"
                                                                           + monthParse(month)
                                                                           + "-" + "01"));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(currentQuarterStartTime);
        return calendarStart.get(Calendar.MONTH);
    }

    /**
     * 获取一个月份的季度结束月份
     *
     * @param month
     *            月份
     * @return 结束月份
     */
    public static Integer getQuarterEndMonth(Integer month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date currentQuarterStartTime;
        try {
            currentQuarterStartTime = getCurrentQuarterEndTime(sdf.parse("2000-"
                                                                         + monthParse(month) + "-"
                                                                         + "01"));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(currentQuarterStartTime);
        return calendarStart.get(Calendar.MONTH);
    }

    /**
     * 月份整形转字符串(补0)
     *
     * @param month
     *            月份
     * @return 两位
     */
    public static String monthParse(Integer month) {
        if (month <= 9) {
            return "0" + month;
        }
        return month.toString();
    }

    // ======new

    /**
     * 获取 当前年、半年、季度、月、日、小时 开始结束时间
     */
    private final static SimpleDateFormat shortSdf;
    private final static SimpleDateFormat longHourSdf;
    private final static SimpleDateFormat longSdf;

    static {
        shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
        longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获得本周的第一天，周一
     *
     * @return
     */
    public Date getCurrentWeekDayStartTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return c.getTime();
    }

    /**
     * 获得本周的最后一天，周日
     *
     * @return
     */
    public Date getCurrentWeekDayEndTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            c.add(Calendar.DATE, 8 - weekday);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return c.getTime();
    }

    /**
     * 获得本天的开始时间，即2012-01-01 00:00:00
     *
     * @return
     */
    public Date getCurrentDayStartTime() {
        Date now = new Date();
        try {
            now = shortSdf.parse(shortSdf.format(now));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 获得本天的结束时间，即2012-01-01 23:59:59
     *
     * @return
     */
    public Date getCurrentDayEndTime() {
        Date now = new Date();
        try {
            now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 获得本小时的开始时间，即2012-01-01 23:59:59
     *
     * @return
     */
    public Date getCurrentHourStartTime() {
        Date now = new Date();
        try {
            now = longHourSdf.parse(longHourSdf.format(now));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 获得本小时的结束时间，即2012-01-01 23:59:59
     *
     * @return
     */
    public Date getCurrentHourEndTime() {
        Date now = new Date();
        try {
            now = longSdf.parse(longHourSdf.format(now) + ":59:59");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 获得本月的开始时间，即2012-01-01 00:00:00
     *
     * @return
     */
    public Date getCurrentMonthStartTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 当前月的结束时间，即2012-01-31 23:59:59
     *
     * @return
     */
    public Date getCurrentMonthEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            c.add(Calendar.MONTH, 1);
            c.add(Calendar.DATE, -1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 获取当前年的开始时间
     *
     * @return 指定年限年的开始时间
     */
    public static Date getCurrentYearStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        if (date != null) {
            c.setTime(date);
        }
        Date now = null;
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 获取当前年的开始时间
     *
     * @return 当前年的开始时间
     */
    public static Date getCurrentYearStartTime() {
        return getCurrentYearStartTime(null);
    }

    /**
     * 当前年的结束时间，即2012-12-31 23:59:59
     *
     * @return
     */
    public Date getCurrentYearEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.MONTH, 11);
            c.set(Calendar.DATE, 31);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 获取指定日期的季度第一天
     *
     * @param date
     *            指定日期
     * @return 指定日期季度第一天
     */
    public static Date getCurrentQuarterStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        if (date != null) {
            c.setTime(date);
        }
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 获取当前季度的开始时间(第一天)
     *
     * @return 当前季度的开始时间
     */
    public static Date getCurrentQuarterStartTime() {
        return getCurrentQuarterStartTime(null);
    }

    /**
     * 获取指定日期的季度的结束时间(最后一天)
     *
     * @return 指定日期的季度的结束时间(最后一天)
     */
    public static Date getCurrentQuarterEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        if (date != null) {
            c.setTime(date);
        }
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                // c.set(Calendar.MONTH,;
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 获取当前季度的结束时间(最后一天)
     *
     * @return 当前季度的结束时间(最后一天)
     */
    public static Date getCurrentQuarterEndTime() {
        return getCurrentQuarterEndTime(null);
    }

    /**
     * 获取指定日期前/后半年的开始时间
     *
     * @param date
     *            指定日期
     * @return 前/后半年的开始时间
     */
    public static Date getHalfYearStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        if (date != null) {
            c.setTime(date);
        }
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 0);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 6);
            }
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;

    }

    /**
     * 获取前/后半年的开始时间
     *
     * @return 前/后半年的开始时间
     */
    public static Date getHalfYearStartTime() {
        return getHalfYearStartTime(null);
    }

    /**
     * 获取指定年限前/后半年的结束时间
     *
     * @param date
     *            指定时间 * @return 前/后半年的结束时间
     */
    public static Date getHalfYearEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        if (date != null) {
            c.setTime(date);
        }
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return now;
    }

    /**
     * 获取<em>今年</em> 前/后半年的结束时间
     *
     * @return 前/后半年的结束时间
     */
    public static Date getHalfYearEndTime() {
        return getHalfYearEndTime(null);
    }

    /**
     * 获取两日期之间相差天数
     *
     * @param startDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @return 天数
     */
    public static long twoDateBetweenDays(Date startDate, Date endDate) {
        return (endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24);
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year
     *            年份
     * @return Date 最后一天日期
     */
    public static Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        currYearLast = setTime(currYearLast, 23, 59, 59);
        return currYearLast;
    }

    /**
     * 设置时分秒
     *
     * @param date
     *            时间
     * @param shi
     *            时
     * @param fen
     *            分
     * @param miao
     *            秒
     * @return 设置好时间
     */
    public static Date setTime(Date date, int shi, int fen, int miao) {
        String ymd = format(YYYY_MM_DD, date);
        ymd += " " + shi + ":" + fen + ":" + miao;
        return parse(YYYY_MM_DD_HH_MM_SS, ymd);
    }

    /**
     * 获取某年的天数
     *
     * @param year
     *            年数
     * @return 次年天数
     */
    public static int getDaysOfYear(Integer year) {
        Calendar cal = Calendar.getInstance();
        if (year != null) {
            cal.set(Calendar.YEAR, year);
        }

        return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
    }

    /**
     * 获取今年的天数
     *
     * @return 次年天数
     */
    public static int getDaysOfYear() {
        return getDaysOfYear(null);
    }

    public static void main(String[] args) {
        Date date = DateUtils.dayAdd(1, new Date());
        System.out.println(date);
    }

    /**
     * 保留日期部分(时分秒设置为0)
     *
     * @param date
     *            日期时间
     * @return yyyy-mm-dd 0:0;0
     */
    public static Date retainDate(Date date) {
        date = dateInit(date);
        Calendar cal = getCalendar();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        return cal.getTime();
    }

    /**
     * 日期设置成当天最后一秒部分(时分秒设置为0)
     *
     * @param date
     *            日期时间
     * @return yyyy-mm-dd 23：59：59
     */
    public static Date dateSetLastSecond(Date date) {
        date = dateInit(date);
        Calendar cal = getCalendar();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MINUTE, 59);
        return cal.getTime();
    }

    /**
     * {@link} dateSetLastSecond
     */
    public static Date dateSetLastSecond() {
        return dateSetLastSecond(null);
    }

    /**
     * {@link} retainDate
     */
    public static Date retainDate() {
        return retainDate(null);
    }

    private static final Calendar calendar = Calendar.getInstance();

    public static Calendar getCalendar() {
        return calendar;
    }

    private static Date dateInit(Date date) {
        if (date == null)
            date = new Date();
        return date;
    }
}